package com.loong.android.tools;

import java.lang.reflect.Array;
import java.util.Date;
import java.util.List;

public class LogUtils {
    public static void log(String tag, String msg, String level) {
        switch (level) {
            case "v":
                android.util.Log.v(tag, msg);
                break;
            case "d":
                android.util.Log.d(tag, msg);
                break;
            case "i":
                android.util.Log.i(tag, msg);
                break;
            case "w":
                android.util.Log.w(tag, msg);
                break;
            case "e":
                android.util.Log.e(tag, msg);
                break;
        }
    }

    public static String convertDataList(Object param, Object... params) {
        StringBuilder output = new StringBuilder();
        output.append(convertData(param));
        if(params != null) {
            for (Object o : params) {
                output.append(" ");
                output.append(convertData(o));
            }
        }
        return output.toString();
    }

    public static String convertData(Object data) {
        if(data == null) return "null";
        if(data instanceof String) return "\"" + data + "\"";
        if(data instanceof Date) return "\"" + data.toString() + "\"";
        if(data instanceof List) {
            List list = (List) data;
            StringBuilder output = new StringBuilder("list(" +list.size()+ ")[");
            boolean isFirst = true;
            for (Object o : list) {
                if(!isFirst) output.append(",");
                output.append(convertData(o));
                isFirst = false;
            }
            output.append("]");
            return output.toString();
        }
        if(data.getClass().isArray()) {
            int len = Array.getLength(data);
            StringBuilder output = new StringBuilder("[");
            boolean isFirst = true;
            for (int i = 0; i < len; i++) {
                Object o = Array.get(data, i);
                if(!isFirst) output.append(",");
                output.append(convertData(o));
                isFirst = false;
            }
            output.append("]");
            return output.toString();
        }
        return "" + data;
    }
}
